import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import {useRightMenuStore} from "@/stores/rightMenu.js";

export const usePathNavigationStore = defineStore('pathNavigation', () => {
  const history = ref([]); // 历史记录数组
  const currentIndex = ref(-1); // 当前索引

  // 记录新的操作
  const recordNewAction = (folderPath) => {
    if (currentIndex.value < history.value.length - 1) {
      history.value = history.value.slice(0, currentIndex.value + 1);
    }
    history.value.push(folderPath);
    currentIndex.value++;
  };

  // 后退操作
  const goBack = async () => {
    currentIndex.value--;
    await useRightMenuStore().openDirectory(currentIndex.value < 0 ? "" : history.value[currentIndex.value],null)
    return true;
  };

  // 前进操作
  const goForward = async () => {
    if (currentIndex.value >= history.value.length - 1) return false; // 无法前进
    currentIndex.value++;
    await useRightMenuStore().openDirectory(history.value[currentIndex.value],null)
    return true;
  };

  // 获取当前文件夹路径
  const getCurrentFolder = () => {
    return history.value[currentIndex.value];
  };

  return {
    recordNewAction,
    goBack,
    goForward,
    getCurrentFolder
  }
})
